﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Ribbon.Web.Helper
{
    public class ControlCollection<TParent, TItem> : System.Web.UI.ControlCollection
        where TParent : Control
        where TItem : Control
    {
        private readonly string _parentName;
        private readonly string _itemName;

        public ControlCollection(TParent owner)
            : base(owner)
        {
            _parentName = typeof (TParent).Name;
            _itemName = typeof (TItem).Name;
        }

        public override void Add(Control child)
        {
            if (!(child is TItem))
            {
                throw new ArgumentException(_parentName + "Collection can only contain "+ _itemName +" controls.");
            }
            base.Add(child);
        }

        public override void AddAt(int index, Control child)
        {
            if (!(child is TItem))
            {
                throw new ArgumentException(_parentName + "Collection can only contain " + _itemName + " controls.");
            }
            base.AddAt(index, child);
        }

        public new TItem this[int index]
        {
            get { return (TItem)base[index]; }
        }
    }
}
